local shefu = fk.CreateSkill {

  name = "joy__shefu",

  tags = {  },

}



shefu:addEffect(fk.EventPhaseStart, {
  name = "joy__shefu",
  anim_type = "control",
  derived_piles = "joy__shefu",
  events ={fk.EventPhaseStart, fk.Damaged,fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(shefu.name) then
      if event == fk.EventPhaseStart then
        return target == player and player.phase == Player.Finish and not player:isNude()
      elseif event == fk.Damaged then
        return target == player and not player:isNude()
      else
        return target ~= player and player.phase == Player.NotActive and data.card.type ~= Card.TypeEquip and U.IsUsingHandcard(target, data)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart or event == fk.Damaged then
      local cards = room:askForCard(player, 1, 1, true, shefu.name, true, ".", "#joy__shefu-cost")
      if #cards > 0 then
        event:setCostData(self, cards[1])
        return true
      end
    else
      local index
      local mark = player:getTableMark( shefu.name)
      for i = 1, #mark, 1 do
        if data.card.trueName == mark[i][2] then
          index = i
          break
        end
      end
      if index and room:askForSkillInvoke(player, shefu.name, nil, "#joy__shefu-invoke::"..target.id..":"..data.card.name) then
        event:setCostData(self, index)
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart or event == fk.Damaged then
      player:addToPile(shefu.name, event:getCostData(self), false, shefu.name)
      local names = {}
      local mark = player:getTableMark( shefu.name)
      for _, id in ipairs(Fk:getAllCardIds()) do
        local card = Fk:getCardById(id)
        if card.type ~= Card.TypeEquip then
          table.insertIfNeed(names, card.trueName)
        end
      end
      if #names > 0 then
        local name = room:askForChoice(player, names, shefu.name)
        table.insert(mark, {event:getCostData(self), name})
        room:setPlayerMark(player, shefu.name, mark)
      end
    else
      local index = event:getCostData(self)
      local mark = player:getTableMark( shefu.name)
      local throw = mark[index][1]
      table.remove(mark, index)
      room:setPlayerMark(player, shefu.name, mark)
      if target.phase ~= Player.NotActive then
        room:setPlayerMark(target, "@@joy__shefu-turn", 1)
      end
      room:moveCards({
        from = player.id,
        ids = {throw},
        toArea = Card.DiscardPile,
        moveReason = fk.ReasonPutIntoDiscardPile,
        skillName = shefu.name,
        specialName = shefu.name,
      })
      data.tos = {}
      room:sendLog{ type = "#CardNullifiedBySkill", from = target.id, arg = shefu.name, arg2 = data.card:toLogString() }
    end
  end,
})
shefu:addEffect(fk.Damaged, {
  name = "joy__shefu",
  anim_type = "control",
  derived_piles = "joy__shefu",
  events ={fk.EventPhaseStart, fk.Damaged,fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(shefu.name) then
      if event == fk.EventPhaseStart then
        return target == player and player.phase == Player.Finish and not player:isNude()
      elseif event == fk.Damaged then
        return target == player and not player:isNude()
      else
        return target ~= player and player.phase == Player.NotActive and data.card.type ~= Card.TypeEquip and U.IsUsingHandcard(target, data)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart or event == fk.Damaged then
      local cards = room:askForCard(player, 1, 1, true, shefu.name, true, ".", "#joy__shefu-cost")
      if #cards > 0 then
        event:setCostData(self, cards[1])
        return true
      end
    else
      local index
      local mark = player:getTableMark( shefu.name)
      for i = 1, #mark, 1 do
        if data.card.trueName == mark[i][2] then
          index = i
          break
        end
      end
      if index and room:askForSkillInvoke(player, shefu.name, nil, "#joy__shefu-invoke::"..target.id..":"..data.card.name) then
        event:setCostData(self, index)
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart or event == fk.Damaged then
      player:addToPile(shefu.name, event:getCostData(self), false, shefu.name)
      local names = {}
      local mark = player:getTableMark( shefu.name)
      for _, id in ipairs(Fk:getAllCardIds()) do
        local card = Fk:getCardById(id)
        if card.type ~= Card.TypeEquip then
          table.insertIfNeed(names, card.trueName)
        end
      end
      if #names > 0 then
        local name = room:askForChoice(player, names, shefu.name)
        table.insert(mark, {event:getCostData(self), name})
        room:setPlayerMark(player, shefu.name, mark)
      end
    else
      local index = event:getCostData(self)
      local mark = player:getTableMark( shefu.name)
      local throw = mark[index][1]
      table.remove(mark, index)
      room:setPlayerMark(player, shefu.name, mark)
      if target.phase ~= Player.NotActive then
        room:setPlayerMark(target, "@@joy__shefu-turn", 1)
      end
      room:moveCards({
        from = player.id,
        ids = {throw},
        toArea = Card.DiscardPile,
        moveReason = fk.ReasonPutIntoDiscardPile,
        skillName = shefu.name,
        specialName = shefu.name,
      })
      data.tos = {}
      room:sendLog{ type = "#CardNullifiedBySkill", from = target.id, arg = shefu.name, arg2 = data.card:toLogString() }
    end
  end,
})
shefu:addEffect(fk.CardUsing, {
  name = "joy__shefu",
  anim_type = "control",
  derived_piles = "joy__shefu",
  events ={fk.EventPhaseStart, fk.Damaged,fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(shefu.name) then
      if event == fk.EventPhaseStart then
        return target == player and player.phase == Player.Finish and not player:isNude()
      elseif event == fk.Damaged then
        return target == player and not player:isNude()
      else
        return target ~= player and player.phase == Player.NotActive and data.card.type ~= Card.TypeEquip and U.IsUsingHandcard(target, data)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart or event == fk.Damaged then
      local cards = room:askForCard(player, 1, 1, true, shefu.name, true, ".", "#joy__shefu-cost")
      if #cards > 0 then
        event:setCostData(self, cards[1])
        return true
      end
    else
      local index
      local mark = player:getTableMark( shefu.name)
      for i = 1, #mark, 1 do
        if data.card.trueName == mark[i][2] then
          index = i
          break
        end
      end
      if index and room:askForSkillInvoke(player, shefu.name, nil, "#joy__shefu-invoke::"..target.id..":"..data.card.name) then
        event:setCostData(self, index)
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart or event == fk.Damaged then
      player:addToPile(shefu.name, event:getCostData(self), false, shefu.name)
      local names = {}
      local mark = player:getTableMark( shefu.name)
      for _, id in ipairs(Fk:getAllCardIds()) do
        local card = Fk:getCardById(id)
        if card.type ~= Card.TypeEquip then
          table.insertIfNeed(names, card.trueName)
        end
      end
      if #names > 0 then
        local name = room:askForChoice(player, names, shefu.name)
        table.insert(mark, {event:getCostData(self), name})
        room:setPlayerMark(player, shefu.name, mark)
      end
    else
      local index = event:getCostData(self)
      local mark = player:getTableMark( shefu.name)
      local throw = mark[index][1]
      table.remove(mark, index)
      room:setPlayerMark(player, shefu.name, mark)
      if target.phase ~= Player.NotActive then
        room:setPlayerMark(target, "@@joy__shefu-turn", 1)
      end
      room:moveCards({
        from = player.id,
        ids = {throw},
        toArea = Card.DiscardPile,
        moveReason = fk.ReasonPutIntoDiscardPile,
        skillName = shefu.name,
        specialName = shefu.name,
      })
      data.tos = {}
      room:sendLog{ type = "#CardNullifiedBySkill", from = target.id, arg = shefu.name, arg2 = data.card:toLogString() }
    end
  end,
})

shefu:addEffect("invalidity", {
  name = "#joy__shefu_invalidity",
  invalidity_func = function(self, from, skill)
    return from:getMark("@@joy__shefu-turn") > 0 and not skill.attached_equip and not skill.name:endsWith("&")
  end
})

return shefu